home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / mfpic / alfatest / mf-revu.tex < prev    next >
Text File  |  1994-07-20  |  12KB  |  326 lines

  1. %%%
  2. %%%  File: mf-revu.tex
  3. %%%
  4. %%%  mfpic 0.2.9 alpha  Tue 21 July 1994
  5. %%%
  6.  
  7. \magnification=\magstep1
  8.  
  9. % heading macros
  10. \newskip\myskip\myskip=10pt plus 1pt
  11. \def\doskip{\vskip\myskip}
  12. \long\def\head#1{\doskip\noindent $\bullet$ {\it #1} \par\doskip\nobreak}
  13. \long\def\subhead#1{\doskip {\sl #1} \par\doskip\nobreak}
  14.  
  15. % abbreviations
  16. \def\LaTeX{{\rm L\raise.42ex\hbox{\kern-.3em a}\kern-.15em\TeX}}
  17. \font\manual=logo10 % may want to use manfnt here instead
  18. \def\MF{{\manual META}\-{\manual FONT}}
  19. \def\PS{{\rm Post}\-{\rm Script}}
  20. \def\texmacro#1{{\tt\char\escapechar #1}}
  21.  
  22. % verbatim text -- extracted from The TeXbook, pages 380--381.
  23. \def\verbatim{\tt
  24.   \def\par{\leavevmode\endgraf}
  25.   \obeylines \obeyspaces}
  26. {\obeyspaces\global\let =\ }
  27.  
  28.  
  29. \centerline{\bf A Review of \MF{} Programming}
  30. \centerline{Geoffrey Tobin ({\tt G.Tobin@ee.latrobe.edu.au}) 21 July 1994}
  31.  
  32. The best reference for \MF{} programming is undoubtedly Professor
  33. Donald E.~Knuth's {\it The \MF{}book}, first published by
  34. Addison-Wesley and copyrighted by the American Mathematical Society
  35. in 1986, ISBN 0-201-13445-4.
  36.  
  37. However, this document provides reminders some facets of \MF{}.
  38.  
  39. \head{Types}
  40.  
  41. \MF{} has nine ($9$) types of expression:  {\tt boolean}, {\tt numeric},
  42. {\tt pair}, {\tt path}, {\tt pen}, {\tt picture}, {\tt string},
  43. {\tt transform}, and {\it vacuous}.  The first eight ($8$) are
  44. {\it named} types; their names can be used to declare variables (for
  45. example, {\tt string surname;}), or to test the type of a variable or
  46. expression (as in {\tt message "x is " \& if pair x: "indeed" else:
  47. "not" fi \& " a pair";}).  Every undeclared \MF{} variable is
  48. {\tt numeric}.
  49.  
  50. Statements can be treated as expressions of the {\it vacuous} type.
  51. Expressions of {\tt boolean} type are either {\tt true} or {\tt false},
  52. whereas {\it vacuous} expressions have only one possible value.
  53. The allowed values of all the types, and the range of expressions
  54. available in \MF{}, are described in {\it The \MF{}book}.
  55.  
  56. \head{Groups}
  57.  
  58. A {\it group} is an {\it compound} expression of the form
  59. {\tt begingroup A; B; C; D endgroup} which may contain several
  60. statements; its value is that of its last expression, so
  61. {\tt begingroup message "hello"; 123.45 endgroup}
  62. has the value of $123.45$, which is {\tt numeric}.
  63. If the group is empty, or if its last item is a semicolon,
  64. then the group has the {\it vacuous} type.
  65.  
  66. \head{Macros}
  67.  
  68. {\it Macros} are the \MF{} equivalent of functions and procedures.
  69. There are two broad classes of macro:  untyped ({\tt def}) and typed
  70. ({\tt vardef}).  Typed macros are permitted to have the {\it vacuous}
  71. type: an example is {\tt vardef fred = message "I am called Fred";
  72. enddef;}\ .  In fact, {\tt vardef A = B enddef;} is effectively a
  73. shorthand for {\tt def A = begingroup B endgroup enddef;}\ .
  74.  
  75. \head{Numerics}
  76.  
  77. \subhead{\MF{} Numeric Operations}
  78.  
  79. The arithmetic operators {\tt +}, {\tt -}, {\tt *}, {\tt /} are
  80. primitives in \MF{}.  So are Pythagorean addition ({\tt ++}) and
  81. subtraction ({\tt +-+}):  $x ++ y = \sqrt {x^2 + y^2}$ and
  82. $x +-+ y = \sqrt {x^2 - y^2}$.
  83.  
  84. Plain \MF{} defines exponentiation ({\tt **}), remainder ({\tt mod})
  85. and integer division ({\tt div}).
  86.  
  87. \subhead{\MF{} Numeric Relations}
  88.  
  89. Numeric expressions can be compared:  relations include equality,
  90. ({\tt =}), and inequalities ({\tt <>}, {\tt >}, {\tt <}, {\tt >=},
  91. {\tt <=}).
  92.  
  93. \subhead{\MF{} Numeric Functions}
  94.  
  95. \MF{} has some useful {\tt numeric} functions built-in.  These
  96. include:  length, sqrt, sind, cosd, mlog, mexp, floor, angle,
  97. uniformdeviate, normaldeviate.
  98.  
  99. {\bf length $x$} is the absolute value of $x$.
  100.  
  101. {\bf sqrt $x$} is the square root of $x$.
  102.  
  103. {\bf sind $x$} is the sine of $x$ degrees.
  104.  
  105. {\bf cosd $x$} is the cosine of $x$ degrees.
  106.  
  107. {\bf angle $(x, y)$} is the arctangent of $y/x$.
  108.  
  109. {\bf mlog $x$} is $256 \hbox{\tt ln} x$, where {\tt ln} is the
  110. logarithm to the base $e$.
  111.  
  112. {\bf mexp $x$} is $e^{x/256}$.
  113.  
  114. {\bf floor $x$} is the largest integer less than or equal to $x$.
  115.  
  116. {\bf uniformdeviate $x$} is a uniformly distributed random number
  117. between $0$ and $x$.
  118.  
  119. {\bf normaldeviate} is a normally distributed random number with mean
  120. $0$ and standard deviation $1$.
  121.  
  122. The random number generators can be restarted at a given stage by
  123. assigning a {\tt numeric} value to the primitive internal variable
  124. {\bf randomseed}.
  125.  
  126. Plain \MF{} defines {\bf ceiling}, {\bf abs} (same as {\bf length}),
  127. {\bf round}, {\bf min} and {\bf max}, among other functions.
  128.  
  129. \head{Pairs}
  130.  
  131. Subsequently, we often speak of {\it points} in the graphical sense.
  132. These are represented in \MF{} by values of type {\tt pair}.
  133. Constants of type {\tt pair} have the form $(x, y)$,
  134. where $x$ and $y$ are both {\tt numeric} constants.
  135. A variable $p$ of type {\tt pair} is equal to the
  136. {\tt pair} expression ({\tt xpart} $p$, {\tt ypart} $p$).
  137.  
  138. \head{Paths}
  139.  
  140. A {\tt path} represents a type of continuous curve called a
  141. {\bf B\'ezier}, which is comprised of a chain of {\bf segments}.
  142. Each segment has a shape determined by four {\bf control points}.
  143. Two of the control points, the {\it key} points, are the segment's
  144. endpoints; often we let \MF{} determine the other two, as in the
  145. {\tt path} expression $p_1..p_2..p_3$, which has two segments,
  146. from $p_1$ to $p_2$, and from $p_2$ to $p_3$.
  147.  
  148. \head{Pens}
  149.  
  150. Pens are a distinctive feature of \MF{}, whereby quite refined
  151. figures can be drawn.  Pens are filled convex shapes that
  152. are moved along {\tt path}s, to alter {\tt picture} pixel values in
  153. often complicated ways.
  154.  
  155. Two pens are initially present in MF{}, {\tt nullpen} and
  156. {\tt pencircle}.  Since {\tt nullpen} is the single point $(0,0)$,
  157. which contains no pixels, it cannot be seen, so it can be used
  158. for filling a region without fuzzing its boundary.  By contrast,
  159. {\tt pencircle} is circular, with the points $(\pm0.5, 0)$ and
  160. $(0, \pm0.5)$ on its circumference.
  161.  
  162. Other pens are constructed as convex polygons via {\tt makepen $c$},
  163. where $c$ is a closed {\tt path}; they key points of $c$ become the
  164. vertices of the pen.
  165.  
  166. Now, {\tt pencircle} and {\tt makepen $c$} are called {\it future}
  167. pens.  They are continuous, whereas pictures are discrete, so pens,
  168. like pairs and paths, must be {\it rendered} discrete before being
  169. applied to a picture.
  170.  
  171. By contrast, {\tt nullpen}, all {\tt pen} variables, and any
  172. pen expression in parentheses, are already {\it discrete} pens.
  173.  
  174. {\it Future} pens can be transformed by scaling, translating, and so
  175. on, just as pairs and paths can; but {\it discrete} pens cannot.
  176. An example of an elliptical future pen is {\tt pencircle xscaled 30
  177. yscaled 20}, the discrete pen for which is illustrated on page 148 of
  178. {\it The \MF{}book}.
  179.  
  180. \head{Pictures}
  181.  
  182. \MF {\tt picture}s contain pixels arranged as a regular grid of
  183. squares.  One of the pixels has as its boundary the {\it unit square},
  184. which is the {\tt path} $(0,0)--(0,1)--(1,1)-(1,0)$.  Pictures are
  185. built up from the {\tt nullpicture}, which has all pixels zero ($0$),
  186. using {\tt addto}, and their pixels are trimmed using {\tt cull}.
  187.  
  188. Picture variables can be assigned:  $v := W;$  where $v$ is a
  189. {\tt picture} variable, and $W$ is a {\tt picture} expression
  190. such as $u + w$, $u - w$, $+u$, $-u$, or combinations of these.
  191. Picture $v$ can have picture $w$ added to it by
  192. {\tt addto $v$ also $w$}, which {\it The MF{}book, page 118} explains
  193. is faster and uses less memory than {\tt $v := v + w$} but is
  194. otherwise the same.
  195.  
  196. The meaning of $v + w$, for example, is a picture in which each pixel
  197. is the sum of the two pixels that occupy the same position in pictures
  198. $v$ and $w$ respectively.  The effects on pixel values of filling and
  199. drawing are much less predictable, and depend greatly on which
  200. {\tt pen} is used.
  201.  
  202. Filling a {\it closed} path $c$ in a picture $v$ uses
  203. {\tt addto $v$ contour $c$ {\it <with list>}},
  204.  
  205. Drawing the outline of a path $f$ uses
  206. {\tt addto $v$ doublepath $c$ {\it <with list>}}.
  207. A {\it <with list>} can be empty, or it can be one or both of the
  208. phrases {\tt withpen {\it <pen expression>}}
  209. and {\tt withweight {\it <numeric expression>}}.
  210. By choosing different pens, drawing is refined, as in calligraphy.
  211. Weights are weighting factors for pixels.
  212.  
  213. To change all pixels in {\tt picture} variable $v$ that have values
  214. between $2$ and $4$ (inclusive) into $-5$, and all other pixels to
  215. zero, use:
  216. {\tt cull $v$ keeping $(2,4)$ withweight $-5$}.
  217. To zero all pixels with values between $-3$ and $1$ (inclusive),
  218. and to change all other pixels into $2$, use:
  219. {\tt cull $v$ dropping $(-3,1)$ withweight $2$},
  220. If the {\tt withweight} phrase is omitted, then the surviving
  221. pixels have value one ($1$).
  222. {\it Beware} of attempting to change zero-valued pixels to nonzero,
  223. as zero-valued pixels extend to infinity in all directions.
  224.  
  225. \head{Transforms}
  226.  
  227. Affine transforms are the natural transformations of Euclidean
  228. geometry:  the linear transformations augmented by translation.
  229. In two dimensions, transforms are completely specified by their
  230. action on three non-collinear points in the plane.  If $T$ is
  231. a {\tt transform}, and $p$ is a pair, then $T$ maps $p$ onto this
  232. {\tt pair}:  $p$ {\tt transformed} $T$.  Thus \MF{} can construct
  233. any affine transform:  for example, plain \MF{} defines the
  234. {\bf identity} transform.  Plain \MF{} also defines a macro named
  235. {\bf inverse} such that
  236. {\bf inverse $T$ transformed $T$ $=$ identity}.
  237.  
  238. Each transform $T$ has six components, and can be expressed as a
  239. linear transformation followed by a translation.  The translation
  240. components of $T$ are {\tt xpart $T$} and {\tt ypart $T$}, and
  241. the four linear components are {\tt xxpart $T$}, {\tt xypart $T$},
  242. {\tt yxpart $T$} and {\tt yypart $T$}.  Employing the obvious
  243. notation, $T$ is applied as:
  244.  
  245. $$
  246.    \left(
  247.      \matrix{t_x \cr t_y}
  248.      \left|
  249.        \vphantom{\matrix{t_xy \cr t_yy}}
  250.      \right.
  251.      \matrix{ t_{xx} & t_{yx} \cr t_{xy} & t_{yy}}
  252.    \right)
  253.    \pmatrix{x \cr y}
  254.    =
  255.    \pmatrix{t_x + t_{xx} x + t_{xy} y \cr
  256.             t_y + t_{yx} x + t_{yy} y}
  257. $$
  258.  
  259. \MF{} also provides seven primitive transforms, listed on page
  260. 141 of {\it The \MF{}book}:
  261.  
  262. \halign
  263. {\indent#\hfil&\quad#\hfil\cr
  264. $(x,y)$ {\tt shifted} $(a,b)$   & $ = (x+a, y+b) $  \cr
  265. $(x,y)$ {\tt scaled}  $s$       & $ = s (x, y) $   \cr
  266. $(x,y)$ {\tt xscaled} $s$       & $ = (sx, y) $    \cr
  267. $(x,y)$ {\tt yscaled} $s$       & $ = (x, sy) $    \cr
  268. $(x,y)$ {\tt slanted} $s$       & $ = (x,y) + s (y, 0) $   \cr
  269. $(x,y)$ {\tt rotated} $\theta$  & $ = (x \cos \theta - y \sin \theta,
  270.                                  x \sin \theta + y \cos \theta) $ \cr
  271. $(x,y)$ {\tt zscaled} $(a,b)$   & $ = (xu - yv, xv + yu) $ \cr
  272. }
  273.  
  274. These operations are taught in secondary school mathematics;
  275. for example, {\tt slanted} is skewing parallel to the X axis.
  276. Interpreting a pair as a complex number in Cartesian coordinates,
  277. {\tt zscaled} is complex multiplication.
  278.  
  279. \head{Composite Variables}
  280.  
  281. \MF{} allows {\it composite} variables --- homogeneous {\it arrays},
  282. heterogeneous {\it suffixes}, and arbitrary combinations of those.
  283. One can declare composite variables, and test whether they
  284. {\it contain} a given structure.  However, these are not fully-fledged
  285. types, since every expression has one of the nine {\it simple} types.
  286. For example, after declaring {\tt path p[];} of what type is {\tt p} ?
  287. It's {\tt numeric}!
  288.  
  289. Moreover, \MF{} {\it lacks} pointers.  It is not possible for a macro
  290. to return a value that has not one of the nine {\it simple} types.
  291. The allowed syntax {\tt vardef b[] = $\cdots$ enddef;} may suggest
  292. that this macro returns an array, but in fact {\tt b[3]} {\it expands}
  293. to the whole {\it replacement text} of the macro.
  294.  
  295. Here is a macro that is useful for testing the type of a variable,
  296. suffix or expression:
  297.  
  298. {
  299. \verbatim
  300.     def type suffix x =
  301.       message str x \& " is "
  302.         \&
  303.         if     known x:     "a known "
  304.         else:               "an unknown "
  305.         fi
  306.         \&
  307.         if     boolean x:   "boolean"
  308.         elseif numeric x:   "numeric"
  309.         elseif pair x:      "pair"
  310.         elseif path x:      "path"
  311.         elseif pen x:       "pen"
  312.         elseif picture x:   "picture"
  313.         elseif string x:    "string"
  314.         elseif transform x: "transform"
  315.         else:               "vacuous"
  316.         fi;
  317.       enddef;
  318.   
  319. }
  320.  
  321. \end
  322.  
  323. %%%
  324. %%%  end  mf-revu.tex
  325. %%%
  326.